<?php
class Forgotten extends CI_Controller {
	private $error = array();

	public function index() {
		if ($this->customer->isLogged()) {
			redirect(site_url('account/account', '', 'SSL'));
		}

		$this->lang->load('account/forgotten');

		$this->document->setTitle($this->lang->line('heading_title'));
		
		$this->load->model('account/Model_Customer');
		
		if (($this->input->server('REQUEST_METHOD') == 'POST') && $this->validate()) {
			$this->lang->load('mail/forgotten');
			
			$password = substr(md5(rand()), 0, 7);
			
			$this->Model_Customer->editPassword($this->input->post('email'), $password);
			
			$subject = sprintf($this->lang->line('text_subject'), $this->cconfig->get('config_name'));
			
			$message  = sprintf($this->lang->line('text_greeting'), $this->cconfig->get('config_name')) . "\n\n";
			$message .= $this->lang->line('text_password') . "\n\n";
			$message .= $password;

			$this->hmail->protocol = $this->cconfig->get('config_mail_protocol');
			$this->hmail->parameter = $this->cconfig->get('config_mail_parameter');
			$this->hmail->hostname = $this->cconfig->get('config_smtp_host');
			$this->hmail->username = $this->cconfig->get('config_smtp_username');
			$this->hmail->password = $this->cconfig->get('config_smtp_password');
			$this->hmail->port = $this->cconfig->get('config_smtp_port');
			$this->hmail->timeout = $this->cconfig->get('config_smtp_timeout');				
			$this->hmail->setTo($this->input->post('email'));
			$this->hmail->setFrom($this->cconfig->get('config_email'));
			$this->hmail->setSender($this->cconfig->get('config_name'));
			$this->hmail->setSubject($subject);
			$this->hmail->setText(html_entity_decode($message, ENT_QUOTES, 'UTF-8'));
			$this->hmail->send();
			
			$this->session->set_userdata('success', $this->lang->line('text_success'));

			redirect(site_url('account/login', '', 'SSL'));
		}

      	$data['breadcrumbs'] = array();

      	$data['breadcrumbs'][] = array(
        	'text'      => $this->lang->line('text_home'),
			'href'      => site_url('common/home'),        	
        	'separator' => false
      	); 

      	$data['breadcrumbs'][] = array(
        	'text'      => $this->lang->line('text_account'),
			'href'      => site_url('account/account', '', 'SSL'),     	
        	'separator' => $this->lang->line('text_separator')
      	);
		
      	$data['breadcrumbs'][] = array(
        	'text'      => $this->lang->line('text_forgotten'),
			'href'      => site_url('account/forgotten', '', 'SSL'),       	
        	'separator' => $this->lang->line('text_separator')
      	);
		
		$data['heading_title'] = $this->lang->line('heading_title');

		$data['text_your_email'] = $this->lang->line('text_your_email');
		$data['text_email'] = $this->lang->line('text_email');

		$data['entry_email'] = $this->lang->line('entry_email');

		$data['button_continue'] = $this->lang->line('button_continue');
		$data['button_back'] = $this->lang->line('button_back');

		if (isset($this->error['warning'])) {
			$data['error_warning'] = $this->error['warning'];
		} else {
			$data['error_warning'] = '';
		}
		
		$data['action'] = site_url('account/forgotten', '', 'SSL');
 
		$data['back'] = site_url('account/login', '', 'SSL');

		$data['header'] = helper_load_header();
		$data['column_left'] = helper_load_left();
		$data['column_right'] = helper_load_right();
		$data['content_top'] = helper_load_top();
		$data['content_bottom'] = helper_load_bottom();
		$data['footer'] = helper_load_footer();
		
		
		$this->load->view("account/forgotten", $data);
	}

	private function validate() {
		if (!$this->input->post('email')) {
			$this->error['warning'] = sprintf($this->lang->line('error_email'), $this->cconfig->get('config_email'), $this->cconfig->get('config_email'));
		} elseif (!$this->Model_Customer->getTotalCustomersByEmail($this->input->post('email'))) {
			$this->error['warning'] = sprintf($this->lang->line('error_email'), $this->cconfig->get('config_email'), $this->cconfig->get('config_email'));
		}

		if (!$this->error) {
			return true;
		} else {
			return false;
		}
	}
}
?>